from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from preceda_global import *
from preceda_genfunc import *
from preceda_fields import *
from preceda_messages import *
from preceda_menus import *
import unittest, time, re, csv, logging, datetime


class preceda_login(unittest.TestCase):
    def setUp(self):
        global tmpwebdriver
        globalflag.flagtocontinue=False

        writelogs(self, globalini.joblog,globalini.csvpath," ")
        writelogs(self, globalini.joblog,globalini.csvpath,MsgClass.msg1)
        writelogs(self, globalini.joblog,globalini.csvpath,MsgClass.msg2+globalini.start_date.strftime("%d-%m-%Y"))
        writelogs(self, globalini.joblog,globalini.csvpath,MsgClass.msg3+globalini.start_time.strftime("%H:%M:%S"))

        # check files and load global variables before process continue
        readtestsuite('testsuite')
        
        tmpwebdriver=globalflag.chbv
        if tmpwebdriver==False:
            self.driver=webdriver.Ie()
            writelogs(self,globalini.joblog,globalini.csvpath,MsgClass.msg24)
        else:
            readrequirements()
            self.base_url="http://210.9.73.12/logon"
            
    def test_preceda(self):
        if tmpwebdriver!=False and globalflag.flagtocontinue!=False:
            global gcntr
            filelist=[]
            gcntr=0
            driver=globalflag.chb2v
            driver.get(self.base_url)
            driver.find_element_by_id("AUSERID").clear()
            driver.find_element_by_id("AUSERID").send_keys(readrequirements.username)
            driver.find_element_by_id("AUSRPWD").clear()
            driver.find_element_by_id("AUSRPWD").send_keys(readrequirements.password)
            driver.find_element_by_id("ACLIENT").clear()
            driver.find_element_by_id("ACLIENT").send_keys(readrequirements.client)
            driver.find_element_by_id("logonButton").click()
            # driver.find_element_by_id("ACLIENT").send_keys(Keys.RETURN)

            driver.implicitly_wait(30)
            # test
            # --------- Start: Check all window handles and transfer focus to the new window ---------
            current=driver.current_window_handle
            writelogs(self,globalini.joblog,globalini.csvpath,MsgClass.msg4+current)
            handles=driver.window_handles
            while len(handles) < 2:
                time.sleep(1)
                handles=driver.window_handles
            if current==handles[1]:
                driver.switch_to_window(handles[0])
                writelogs(self,globalini.joblog,globalini.csvpath,MsgClass.msg5+handles[0])
            else:
                driver.switch_to_window(handles[1])
                writelogs(self,globalini.joblog,globalini.csvpath,MsgClass.msg5+handles[1])
            # --------- End: Check all window handles and transfer focus to the new window ---------

            runtestsuite('testsuite')
            
            if globalflag.flagtocontinue==False:
                print "Test Failed: Problem accessing menus. Ending automation test."
            else:
                # tmpaction=getfields("Administration;Reference;Base Details;Projects","","New","Project ID=00001;Commencement=01012013")
                if globalflag.flagtocontinue==4:
                    print "Test Failed: Invalid field name, not visible, or field not found. Ending automation test."
                elif globalflag.flagtocontinue==2:
                    print "Test Failed: One of the action button invalid. Ending automation test."
                elif globalflag.flagtocontinue==3:
                    print "Test Failed: One of the action button is hidden or read-only. Ending automation test."
                elif globalflag.flagtocontinue==5:
                    print "Test Failed: Test script not found. Ending automation test."
                elif globalflag.flagtocontinue==6:
                    print "Test Failed: Saving failed to execute. Ending automation test."
                elif globalflag.flagtocontinue==7:
                    print "Test Failed: Failed to search record. Ending automation test."
                elif globalflag.flagtocontinue==8:
                    print "Test Failed: Refresh action failed. Ending automation test."
                elif globalflag.flagtocontinue==9:
                    print "Test Failed: Delete action failed. Ending automation test."
                elif globalflag.flagtocontinue==10:
                    print "Test Failed: Script error message. Ending automation test."
                else:
                    print "Automation Test Status: Pass"
            
    def tearDown(self):
        driver=globalflag.chb2v
        writelogs(self,globalini.joblog,globalini.csvpath,MsgClass.msg9+datetime.datetime.now().date().strftime("%d-%m-%Y"))
        writelogs(self,globalini.joblog,globalini.csvpath,MsgClass.msg10+datetime.datetime.now().time().strftime("%H:%M:%S"))
        if tmpwebdriver!=False:
            time.sleep(5)
            if globalini.closeonend==1: driver.quit()
        else: driver.quit()

if __name__ == "__main__":
    unittest.main()
